import {Ref, ref} from "vue";
import {defineStore} from "pinia";
import {getHospitalAPI, getHospitalLevelAndRegionAPI, searchHospitalNameAPI} from "@/api/homeApi.ts";
import {hospitalInterface, pageInterface} from "@/views/home/Interface/Interface.ts";

const useHomeStore = defineStore('homeStore', () => {
    const hospitalListInfo: Ref<any> = ref({})
    const levelList: Ref<any> = ref([])
    const regionList: Ref<any> = ref([])
    const hosNameList: Ref<hospitalInterface[]> = ref([])
    const fetchHospitalList = async (pageParams: pageInterface) => {
        const {data: hospitalList} = await getHospitalAPI(pageParams)
        hospitalListInfo.value = hospitalList
        return hospitalList
    }
    const fetchLevelList = async (dictCode: string) => {
        const {data: level} = await getHospitalLevelAndRegionAPI(dictCode)
        levelList.value = level
        return level
    }
    const fetchRegionList = async (dictCode: string) => {
        const {data: region} = await getHospitalLevelAndRegionAPI(dictCode)
        regionList.value = region
        return region
    }

    const searchHospitalNameList = async (hosname: string) => {
        const {data: HospitalName} = await searchHospitalNameAPI(hosname)
        const result = HospitalName.map(item => ({hoscode: item.hoscode, value: item.hosname}))
        hosNameList.value = result
        return result
    }
    return {
        hospitalListInfo,
        levelList,
        regionList,
        hosNameList,
        fetchHospitalList,
        fetchLevelList,
        fetchRegionList,
        searchHospitalNameList
    }
})
export default useHomeStore
